package com.blb.community.controller;


import com.blb.community.Annotation.Log;
import com.blb.community.dto.SysUserDto;
import com.blb.community.entity.SysDictData;
import com.blb.community.entity.SysPost;
import com.blb.community.service.ISysPostService;
import com.blb.community.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 岗位信息表 前端控制器
 * </p>
 *
 * @author 帅帅
 * @since 2022-07-02
 */
@Api(tags = "岗位模块")
@RestController
@RequestMapping("/post")
public class SysPostController {

    @Resource
    private ISysPostService sysPostService;

    @ApiOperation("查询所有岗位")
    @GetMapping("/getPostAll")
    public ResponseResult<List<SysPost>> getAll() {
        return ResponseResult.ok(sysPostService.getAll());
    }

    /**
     * 查询结果集
     *
     * @param sysPost 封装对象（对象实体+当前页码+页面数据量大小）
     * @return HashMap（键值对存储）
     */
    @ApiOperation("模糊查询")

    @PostMapping("/selectPostList")
    public ResponseResult<Map<Object, Object>> selectPostList(@RequestBody SysPost sysPost) {
        HashMap<Object, Object> map = new HashMap<>();
        List<SysPost> postList = sysPostService.selectPostList(sysPost, 0, 0);

        //总条数
        int total = postList.size();
        int pages = total % sysPost.getPageSize() == 0 ? total / sysPost.getPageSize() : total / sysPost.getPageSize() + 1;
        if (sysPost.getNowPage() > pages) {
            sysPost.setNowPage(pages);
        }
        //页码修正
        if (sysPost.getNowPage() < 1) {
            sysPost.setNowPage(1);
        }
        //重新赋值
        postList = sysPostService.selectPostList(sysPost, sysPost.getNowPage(), sysPost.getPageSize());

        map.put("postList", postList);
        map.put("total", total);
        map.put("pages", pages);
        map.put("nowPage", sysPost.getNowPage());
        map.put("pageSize", sysPost.getPageSize());

        return ResponseResult.ok(map);
    }

    /**
     * 添加岗位
     *
     * @param sysPost
     * @return
     */
    @Log(module = "添加岗位", operationType = 1, requestMethod = "POST")
    @ApiOperation("添加岗位")
    @PostMapping("/addPost")
    public ResponseResult<SysUserDto<SysPost>> insertPost(@RequestBody SysPost sysPost) {
        //new出Dto存状态码
        SysUserDto<SysPost> sysUserSysUserDto = new SysUserDto<>();
        int i = sysPostService.insertPost(sysPost);
        if (i == 0) {
            sysUserSysUserDto.setCode(201);
        } else if (i == 1) {
            sysUserSysUserDto.setCode(200);
        } else if (i == 2) {
            sysUserSysUserDto.setCode(201);
            sysUserSysUserDto.setMsg("岗位名称已存在！");
        } else if (i == 3) {
            sysUserSysUserDto.setCode(201);
            sysUserSysUserDto.setMsg("岗位编码已存在！");
        } else if (i == 4) {
            sysUserSysUserDto.setCode(201);
            sysUserSysUserDto.setMsg("岗位名称和编码已存在！");
        }
        return ResponseResult.ok(sysUserSysUserDto);
    }

    /**
     * 修改岗位
     *
     * @param sysPost
     * @return
     */
    @Log(module = "修改岗位", operationType = 2, requestMethod = "PUT")
    @ApiOperation("修改岗位")
    @PutMapping("/updatePost")
    public ResponseResult<SysUserDto<SysPost>> updatePost(@RequestBody SysPost sysPost) {
        //new出Dto存状态码
        SysUserDto<SysPost> sysUserSysUserDto = new SysUserDto<>();
        int i = sysPostService.updatePost(sysPost);
        if (i == 0) {
            sysUserSysUserDto.setCode(201);
        } else if (i == 1) {
            sysUserSysUserDto.setCode(200);
        } else if (i == 2) {
            sysUserSysUserDto.setCode(201);
            sysUserSysUserDto.setMsg("岗位名称已存在！");
        } else if (i == 3) {
            sysUserSysUserDto.setCode(201);
            sysUserSysUserDto.setMsg("岗位编码已存在！");
        } else if (i == 4) {
            sysUserSysUserDto.setCode(201);
            sysUserSysUserDto.setMsg("岗位名称和编码已存在！");
        } else if (i == 5) {
            sysUserSysUserDto.setCode(201);
            sysUserSysUserDto.setMsg("岗位被分配，不能停用！");
        }

        return ResponseResult.ok(sysUserSysUserDto);
    }

    /**
     * 根据岗位id单删除岗位信息
     *
     * @param postId
     * @return
     */
    @Log(module = "根据岗位id单删除岗位信息", operationType = 3, requestMethod = "DELETE")
    @ApiOperation("根据岗位id单删除岗位信息")
    @DeleteMapping("/delPostId")
    public ResponseResult<SysUserDto<SysPost>> deletePostById(Integer postId) {
        //new出Dto存状态码
        SysUserDto<SysPost> sysUserSysUserDto = new SysUserDto<>();
        int i = sysPostService.deletePostById(postId);
        if (i == 3) {
            sysUserSysUserDto.setCode(201);
            sysUserSysUserDto.setMsg("删除项中存在岗位分配情况！");
        } else {
            sysUserSysUserDto.setCode(200);
        }
        return ResponseResult.ok(sysUserSysUserDto);
    }


    /**
     * 批量删除岗位
     *
     * @param postIds
     * @return
     */
    @Log(module = "批量删除岗位", operationType = 3, requestMethod = "DELETE")
    @ApiOperation("批量删除岗位")
    @PostMapping("/delPost")
    public ResponseResult<SysUserDto<SysPost>> deletePost(@RequestBody Integer[] postIds) {
        //new出Dto存状态码
        SysUserDto<SysPost> sysUserSysUserDto = new SysUserDto<>();
        int i = sysPostService.deletePostByIds(postIds);
        if (i == 3) {
            sysUserSysUserDto.setCode(201);
            sysUserSysUserDto.setMsg("删除项中存在岗位分配情况！");
        } else {
            sysUserSysUserDto.setCode(200);
        }
        return ResponseResult.ok(sysUserSysUserDto);
    }

    /**
     * 获取使用状态（正常/停用）
     *
     * @return
     */
    @ApiOperation("获取使用状态（正常/停用）")
    @GetMapping("/getDict")
    public ResponseResult<Object[]> getDict() {
        List<SysDictData> dict = sysPostService.getDict((long) 3);
        return ResponseResult.ok(dict.toArray());
    }

    /**
     * 岗位数据导出
     * @param postIds
     * @param response
     */
//    @ApiOperation("岗位数据导出")
//    @PostMapping("/getExcel")
//    @SneakyThrows
//    public void simpleWritea(@RequestBody ArrayList<Integer> postIds, HttpServletResponse response) {
//        List<SysPost> sysPosts = sysPostService.selectByIds(postIds);
//        System.out.println("TESTEXECl");
//        for (SysPost sysPost : sysPosts) {
//            System.out.println("sysPost = " + sysPost);
//        }
//        String fileName = URLEncoder.encode("岗位表数据", "UTF-8");
//        response.setContentType("application/vnd.ms-excel");
//        response.setCharacterEncoding("utf-8");
//        response.setHeader("content-type", "text/html;charset=UTF-8");
//        // 内容样式
//        HorizontalCellStyleStrategy horizontalCellStyleStrategy = ContentStyle.getContentStyle();
//        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
//        EasyExcel.write(response.getOutputStream(), SysPost.class)
//                .excelType(ExcelTypeEnum.XLS)
//                //自适应表格格式
//                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
//                .autoCloseStream(true)
//                .sheet("模板")
//                .doWrite(sysPosts);
//    }

}
